package com.fanketly.funacg2.ui.components

import android.util.Log
import androidx.compose.foundation.layout.*
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Devices
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.fanketly.funacg2.R
import com.fanketly.funacg2.data.TAG
import com.fanketly.funacg2.ui.theme.FunACG2Theme
import com.fanketly.funacg2.utils.TimeUtils
import com.fanketly.funacg2.utils.ToastUtils

@Composable
fun TimerDialog(
    stopRemainingTime: Long,
    onDismiss: () -> Unit,
    onConfirm: (Long) -> Unit,
    onDismissRequest: () -> Unit,
) {
    val millisecondToMin = TimeUtils.millisecondToMin(stopRemainingTime)
    Log.i(TAG, "TimerDialog:传入的毫秒数：$stopRemainingTime ,转换后的分钟数：$millisecondToMin")
    var hour by remember {
        mutableStateOf(millisecondToMin / 60)
    }
    var min by remember {
        mutableStateOf(millisecondToMin % 60)
    }
    AlertDialog(onDismissRequest = onDismissRequest, confirmButton = {
        TextButton(
            onClick = {
                ToastUtils.shortToast("将在 $hour:$min 后关闭")
                onConfirm(TimeUtils.minToMillisecond(min) + TimeUtils.hourToMillisecond(hour))
            }
        ) {
            Text(stringResource(id = R.string.confirm))
        }
    }, dismissButton = {
        TextButton(onClick = onDismiss) {
            Text(stringResource(id = R.string.dismiss))
        }
    }, title = {
        Text(
            text = "定时关闭",
            style = MaterialTheme.typography.titleLarge,
            modifier = Modifier.padding(bottom = 16.dp)
        )
    }, text = {
        //占位
        Text(
            text = "",
            style = MaterialTheme.typography.titleMedium,
        )
        Row(
            verticalAlignment = Alignment.CenterVertically,
            horizontalArrangement = Arrangement.Center
        ) {
            Column(horizontalAlignment = Alignment.CenterHorizontally) {//小时区
                TextButton(
                    onClick = {
                        hour += TimeUtils.plusOneHourButMax24(hour)
                    }, colors = ButtonDefaults.textButtonColors(
                        containerColor = MaterialTheme.colorScheme.primary,
                    ), shape = MaterialTheme.shapes.medium
                ) {
                    Text(
                        text = "+1",
                        color = MaterialTheme.colorScheme.onPrimary,
                        fontSize = 30.sp
                    )
                }
                Text(
                    text = if (hour < 10) "0$hour" else "$hour",
                    modifier = Modifier.width(120.dp),
                    fontSize = 80.sp,
                    textAlign = TextAlign.Center
                )
                TextButton(
                    onClick = {
                        hour -= TimeUtils.negOneHourButMin0(hour)
                    }, colors = ButtonDefaults.textButtonColors(
                        containerColor = MaterialTheme.colorScheme.primary,
                    ), shape = MaterialTheme.shapes.medium
                ) {
                    Text(
                        text = "-1",
                        color = MaterialTheme.colorScheme.onPrimary,
                        fontSize = 30.sp
                    )
                }
            }
            Text(
                text = ":", modifier = Modifier.width(20.dp), fontSize = 80.sp
            )
            Column(horizontalAlignment = Alignment.CenterHorizontally) {//分钟区
                Row {
                    TextButton(//加10分钟
                        onClick = {
                            if (min + 10 >= 60) {
                                min = min + 10 - 60
                                hour += TimeUtils.plusOneHourButMax24(hour)
                            } else {
                                min += 10
                            }
                        }, colors = ButtonDefaults.textButtonColors(
                            containerColor = MaterialTheme.colorScheme.primary,
                        ), shape = MaterialTheme.shapes.medium, modifier = Modifier.size(56.dp)
                    ) {
                        Text(
                            text = "+1",
                            color = MaterialTheme.colorScheme.onPrimary,
                            fontSize = 26.sp
                        )
                    }
                    Spacer(modifier = Modifier.width(4.dp))
                    TextButton(//加1分钟
                        onClick = {
                            if (min + 1 >= 60) {
                                min = min + 1 - 60
                                hour += TimeUtils.plusOneHourButMax24(hour)
                            } else {
                                min += 1
                            }
                        }, colors = ButtonDefaults.textButtonColors(
                            containerColor = MaterialTheme.colorScheme.primary,
                        ), shape = MaterialTheme.shapes.medium, modifier = Modifier.size(56.dp)
                    ) {
                        Text(
                            text = "+1",
                            color = MaterialTheme.colorScheme.onPrimary,
                            fontSize = 26.sp
                        )
                    }
                }
                Text(
                    text = if (min < 10) "0$min" else "$min",
                    modifier = Modifier.width(120.dp),
                    fontSize = 80.sp,
                    textAlign = TextAlign.Center
                )
                Row {
                    TextButton(//减10分钟
                        onClick = {
                            min -= TimeUtils.negTenMinButMin0(min)
                        }, colors = ButtonDefaults.textButtonColors(
                            containerColor = MaterialTheme.colorScheme.primary,
                        ), shape = MaterialTheme.shapes.medium
                    ) {
                        Text(
                            text = "-1",
                            color = MaterialTheme.colorScheme.onPrimary,
                            fontSize = 30.sp
                        )
                    }
                    Spacer(modifier = Modifier.width(4.dp))
                    TextButton(//减1分钟
                        onClick = {
                            min -= TimeUtils.negOneMinButMin0(min)
                        }, colors = ButtonDefaults.textButtonColors(
                            containerColor = MaterialTheme.colorScheme.primary,
                        ), shape = MaterialTheme.shapes.medium
                    ) {
                        Text(
                            text = "-1",
                            color = MaterialTheme.colorScheme.onPrimary,
                            fontSize = 30.sp
                        )
                    }
                }
            }
        }
    })
}

//@Composable
//fun TimerDialog(
//    openDialog: Boolean,
//    hour: Int,
//    min: Int,
//    onDismiss: () -> Unit,
//    onConfirm: (Long) -> Unit,
//    onDismissRequest: () -> Unit,
//    onPlusOneMin: () -> Unit = {},
//    onPlusTenMin: () -> Unit = {},
//    onPlusOneHour: () -> Unit = {},
//    onNegOneMin: () -> Unit = {},
//    onNegTenMin: () -> Unit = {},
//    onNegOneHour: () -> Unit = {},
//) {
//    if (openDialog)
//        AlertDialog(
//            onDismissRequest = onDismissRequest,
//            confirmButton = {
//                TextButton(
//                    onClick = onDismiss
//                ) {
//                    Text(stringResource(id = R.string.confirm))
//                }
//            },
//            dismissButton = {
//                TextButton(
//                    onClick = {
//                        onConfirm(1)
//                    }
//                ) {
//                    Text(stringResource(id = R.string.dismiss))
//                }
//            },
//            title = {
//                   Surface {
//                       Text(
//                           text = "定时关闭",
//                           style = MaterialTheme.typography.titleMedium,
//                           modifier = Modifier.padding(bottom = 16.dp)
//                       )
//                   }
//            },
//            text = {
//                Column(
//                    horizontalAlignment = Alignment.CenterHorizontally,
//                    verticalArrangement = Arrangement.Bottom,
//                    modifier = Modifier
//                        .fillMaxWidth()
//                        .wrapContentHeight()
//                ) {
//                    Text(
//                        text = "",
//                        style = MaterialTheme.typography.titleMedium,
//                    )
//                    Row {
//                        PlusOneIconButton(
//                            modifier = Modifier
//                                .width(30.dp)
//                                .height(30.dp),
//                            onClick = onPlusOneHour
//                        )
//                        Spacer(modifier = Modifier.width(10.dp))
//                        PlusOneIconButton(modifier = Modifier.size(30.dp), onClick = onPlusTenMin)
//                        Spacer(modifier = Modifier.width(4.dp))
//                        PlusOneIconButton(modifier = Modifier.size(30.dp), onClick = onPlusOneMin)
//                    }
//                    Row(
//                        horizontalArrangement = Arrangement.Center,
//                        verticalAlignment = Alignment.CenterVertically
//                    ) {
//                        Text(
//                            text = "$hour",
//                            modifier = Modifier.width(48.dp),
//                            fontSize = 34.sp,
//                            textAlign = TextAlign.Center
//                        )
//                        Text(
//                            text = ":", modifier = Modifier
//                                .width(10.dp), fontSize = 34.sp
//                        )
//                        Text(
//                            text = "$min",
//                            modifier = Modifier.width(48.dp),
//                            fontSize = 34.sp,
//                            textAlign = TextAlign.Center
//                        )
//                    }
//                    Row {
//                        NegOneIconButton(
//                            modifier = Modifier
//                                .width(30.dp)
//                                .height(30.dp),
//                            onClick = onNegOneHour
//                        )
//                        Spacer(modifier = Modifier.width(10.dp))
//                        NegOneIconButton(modifier = Modifier.size(30.dp), onClick = onNegTenMin)
//                        Spacer(modifier = Modifier.width(4.dp))
//                        NegOneIconButton(modifier = Modifier.size(30.dp), onClick = onNegOneMin)
//                    }
//                }
//
//            })
//}


@Preview(device = Devices.PIXEL)
//@Preview(device = Devices.PIXEL_4)
//@Preview(device = Devices.PIXEL_3_XL)
@Composable
private fun DialogPreview() {
    FunACG2Theme {
        TimerDialog(0, onDismiss = { }, onConfirm = {}) {}
    }
}